﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;

namespace DataLib.DAL
{
    /// <summary>
    /// 数据访问类:StorageData
    /// </summary>
    public partial class StorageData
    {
       
        #region  ExtensionMethod

        /// <summary>
        /// 取得当前的期末净重
        /// </summary>
        /// <param name="goodsCode"></param>
        /// <returns></returns>
        public decimal CurEndNwt(string goodsCode, string strno)
        {
            DataTable dt = new DataTable();
            decimal curnwt = 0;
            dt = GetList(1, "goodsCode='" + goodsCode + "' and storageno='" + strno + "' ", "id desc").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
                curnwt = decimal.Parse(dt.Rows[0]["endnwt"].ToString());
            return curnwt;
        }

        /// <summary>
        /// 获取某段时间内的库存
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="gcode">物品编码</param>
        /// <param name="strbelong">归属地</param>
        /// <param name="strdis">物理地</param>
        /// <param name="stroageno">仓库编号</param>
        /// <returns></returns>
        public DataTable getDsByDate(DateTime startdate, DateTime enddate, string strbelong, string strdis, string gcode, string stroageno)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@BelongCity",SqlDbType.VarChar,30),
                new SqlParameter("@GraphiCity",SqlDbType.NVarChar,30),
                new SqlParameter("@StartDate",SqlDbType.DateTime,30),
                new SqlParameter("@EndDate",SqlDbType.DateTime,30),
                new SqlParameter("@goodsCode",SqlDbType.NVarChar,30),
                new SqlParameter("@StorageNo",SqlDbType.NVarChar,30)
             };
            sqlparameter[0].Value = strbelong;
            sqlparameter[1].Value = strdis;
            sqlparameter[2].Value = startdate;
            sqlparameter[3].Value = enddate;
            sqlparameter[4].Value = gcode;
            sqlparameter[5].Value = stroageno;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_GetStorageTotal", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;

        }

        /// <summary>
        /// 根据虚拟Load号取得Load号总重量
        /// </summary>
        /// <param name="vloadnum">虚拟Load号</param>
        /// <param name="flag">0是Load单总重量，否则是已入库总重量</param>
        /// <returns></returns>
        public string GetLoadWeight(string vloadnum, int flag)
        {

            string weight = "0";
            if (vloadnum.IndexOf("86") == 0)
            {
                DataTable dt = new LoadNum().GetList("loadnum='" + vloadnum + "'").Tables[0];
                if (dt != null && dt.Rows.Count > 0 && dt.Rows[0]["totalweight"] != null)
                {
                    if (flag == 0)
                    {
                        weight = dt.Rows[0]["totalweight"].ToString();
                    }
                    else
                    {
                        weight = dt.Rows[0]["finishweight"].ToString();
                    }
                }
            }
            else
            {
                string strsql = @"SELECT Sum([totalWeight]) FROM [EWTS].[dbo].[OutStorage] where [VLoadNum] = '" + vloadnum + "'";
                DataSet ds = DbHelperSQL.Query(strsql);
                if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0] != null)
                {
                    weight = ds.Tables[0].Rows[0][0].ToString();
                }

            }
            if (weight == null && weight.Trim() == "")
                weight = "0.00";
            return weight;
        }

        /// <summary>
        /// 根据虚拟Load获取入库所有日期
        /// </summary>
        /// <param name="vLoadNum">虚拟Load号</param>
        /// <param name="flag">0代表正文显示，其他值代表Title，鼠标移上去的提示</param>
        /// <returns></returns>
        public string InStorageDate(string vLoadNum, int flag)
        {
            string strdate = "";
            string strsql = "select * from InStorage where id in (select InStrorageID from BarCode where ParmentID in (select id from OutStorage where VLoadNum='" + vLoadNum + "')) order by id desc";
            DataTable dt = DbHelperSQL.Query(strsql).Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["instoragetime"] != null)
                {
                    DateTime dtime = DateTime.Parse(dt.Rows[0]["instoragetime"].ToString());
                    strdate += dtime.ToString("yyyy-MM-dd");
                }

                if (dt.Rows.Count > 1)
                {
                    if (flag == 0)
                    {
                        strdate += "...";
                    }
                    else
                    {
                        for (int i = 1; i < dt.Rows.Count; i++)
                        {
                            DateTime dtime = DateTime.Parse(dt.Rows[0]["instoragetime"].ToString());
                            strdate += "," + dtime.ToString("yyyy-MM-dd");
                        }
                    }
                }



            }
            return strdate;
        }

      


        /// <summary>
        /// 获取某段时间内的库存明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">归属地</param>
        /// <param name="strdis">物理地</param>
        /// <returns></returns>
        public DataTable StorageItemsQuery(string enddate, string bcity, string gcity)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@bcity",SqlDbType.VarChar,20),
                new SqlParameter("@gcity",SqlDbType.VarChar,20),
                new SqlParameter("@enddate",SqlDbType.DateTime,30),
             };
            sqlparameter[0].Value = bcity;
            sqlparameter[1].Value = gcity;
            sqlparameter[2].Value = enddate;
            SqlDataReader dr = DbHelperSQL.RunProcedure("EWMS_StoageItemsQuery", sqlparameter);
            dt.Load(dr);
            dr.Close();
            return dt;

        }


        /// <summary>
        /// 获取某段时间内的库存明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">归属地</param>
        /// <param name="strdis">物理地</param>
        /// <returns></returns>
        public DataTable StorageItemsQueryPage(string enddate, string bcity, string gcity, int currpage, int length, out int totalRecord)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@bcity",SqlDbType.VarChar,20),
                new SqlParameter("@gcity",SqlDbType.VarChar,20),
                new SqlParameter("@enddate",SqlDbType.VarChar,30),
                new SqlParameter("@currpage",SqlDbType.Int),
                new SqlParameter("@Length",SqlDbType.Int),
                new SqlParameter("@totalcount",SqlDbType.Int),
                 new SqlParameter("@isexport",SqlDbType.Int)
            };
            sqlparameter[0].Value = bcity;
            sqlparameter[1].Value = gcity;
            sqlparameter[2].Value = enddate;
            sqlparameter[3].Value = currpage;
            sqlparameter[4].Value = length;
            sqlparameter[5].Direction = ParameterDirection.Output;
            sqlparameter[6].Value = 0;
            dt= DbHelperSQL.RunProcedure("EWMS_StoageItemsQuery", sqlparameter, "t1",300).Tables[0];
            //dt.Load(dr);
            //dr.Close();
             totalRecord = int.Parse(sqlparameter[5].Value.ToString());
            return dt;
        }

        /// <summary>
        /// 获取某段时间内的库存明细
        /// </summary>
        /// <param name="startdate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="strbelong">归属地</param>
        /// <param name="strdis">物理地</param>
        /// <returns></returns>
        public DataTable GetStorageItemsQuery(string enddate, string bcity, string gcity)
        {
            DataTable dt = new DataTable();
            SqlParameter[] sqlparameter ={
                new SqlParameter("@bcity",SqlDbType.VarChar,20),
                new SqlParameter("@gcity",SqlDbType.VarChar,20),
                new SqlParameter("@enddate",SqlDbType.VarChar,30),
                new SqlParameter("@currpage",SqlDbType.Int),
                new SqlParameter("@Length",SqlDbType.Int),
                new SqlParameter("@totalcount",SqlDbType.Int),
                 new SqlParameter("@isexport",SqlDbType.Int)
            };
            sqlparameter[0].Value = bcity;
            sqlparameter[1].Value = gcity;
            sqlparameter[2].Value = enddate;
            sqlparameter[3].Value = 0;
            sqlparameter[4].Value = 0;
            sqlparameter[5].Direction = ParameterDirection.Output;
            sqlparameter[6].Value = 1;
            dt = DbHelperSQL.RunProcedure("EWMS_StoageItemsQuery", sqlparameter, "t1", 100).Tables[0];
            //dt.Load(dr);
            //dr.Close();

            return dt;
        }
      
        #endregion  ExtensionMethod
    }
}
